package com.ziyun.erp.common.api.logistics;

import com.utility.StringUtils;
import com.ziyun.erp.common.utils.HttpUtilsAl;
import com.ziyun.erp.common.utils.JsonUtils;
import io.renren.common.utils.R;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 物流查询api
 * @author zh
 * @date 2018/11/159:28
 */
@Component
public class LogisticsSearchApi {

    @Value("${logistics.host}")
    private String host;

    @Value("${logistics.path}")
    private String path;

    @Value("${logistics.appcode}")
    private String appcode;

    public R httpSearchLogistics(String nu, String type){
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("no", nu);
        querys.put("type", type);
        try{
            HttpResponse response = HttpUtilsAl.doGet(host, path, "GET", headers, querys);
            String rs = EntityUtils.toString(response.getEntity());
            if (StringUtils.isBlank(rs)){
                throw new Exception("请求物流查询返回结果解异!");
            }
            LogisticsVo lv = JsonUtils.string2Object(rs, LogisticsVo.class);
            return R.ok().put("data", lv);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }


}
